package com.siwei.mes.service.order.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.order.OrderClock;
import com.siwei.mes.enums.DynamicEnum;
import com.siwei.mes.mapper.order.OrderClockMapper;
import com.siwei.mes.mapper.order.OrderMapper;
import com.siwei.mes.service.order.OrderClockService;
import com.siwei.mes.service.order.OrderDynamicService;
import com.siwei.mes.service.system.AuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工单打卡(OrderClock)表服务实现类
 *
 * @author makejava
 * @since 2023-12-05 09:31:33
 */
@Service
public class OrderClockServiceImpl implements OrderClockService {
    @Resource
    private OrderClockMapper orderClockMapper;
    @Resource
    private AuthService authService;
    @Resource
    private OrderDynamicService orderDynamicService;
    @Resource
    private OrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(OrderClock entity) {
        assertTrue(orderMapper.loadById(entity.getOrderId()) == null, "工单信息不存在");
        entity.setUserId(authService.getLoginUserId());
        entity.setUserName(authService.getLoginUserName());
        entity.setCreateTime(new Date());
        orderClockMapper.insertSelective(entity);
        orderDynamicService.insertLogClock(entity.getOrderId(), entity.getId(), DynamicEnum.ORDER_CLOCK);
        return entity.getId();
    }

    @Override
    public void update(OrderClock entity) {
        OrderClock orderClock = orderClockMapper.selectByPrimaryKey(entity.getId());
        assertTrue(orderClock == null, "工单打卡信息不存在");
        orderClockMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        OrderClock orderClock = orderClockMapper.selectByPrimaryKey(id);
        assertTrue(orderClock == null, "工单打卡信息不存在");
        orderClockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<OrderClock> getPage(PageQuery<OrderClock, OrderClock> query) {
        OrderClock searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> orderClockMapper.getList(searchParam));
    }

    @Override
    public OrderClock loadById(Long id) {
        return orderClockMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderClock> getAll(OrderClock entity) {
        return orderClockMapper.getList(entity);
    }

    @Override
    public OrderClock getOrderClockByOrderId(OrderClock entity) {
        List<OrderClock> orderClockList = orderClockMapper.getList(entity);
        if (CollectionUtil.isNotEmpty(orderClockList)) {
            return orderClockList.get(0);
        }
        return new OrderClock();
    }
}
